#!/usr/bin/perl

use strict;
use File::Basename;
use File::Spec;
use File::Glob;
use Data::Dumper;
use Config::File;

my ($name, $path) = fileparse($0);

my $base_dir = File::Spec->rel2abs($path);
my $modules_dir = "$base_dir/modules";
my $scripts_dir = "$base_dir/scripts";

print "base: $base_dir\nmodules: $modules_dir\n";
my @module_files = glob "$modules_dir/*";

# Read the modules
my $modules;
for(@module_files)
{
    my $name = fileparse($_);
    $modules->{$name} = Config::File::read_config_file($_);
}

# Figure out which ones we can use and what categories we have
print "\nDetecting what power savings features are available:\n";
my $categories;
for(keys %$modules)
{
    my $category = %$modules->{$_}{category};
    my $name = %$modules->{$_}{name};

    my $failed = system("sh $scripts_dir/".%$modules->{$_}{detect});
    %$modules->{$_}{available} = $failed ? 0 : 1;
    %$categories->{$category} = [] unless defined %$categories->{$category};

    push @{%$categories->{$category}}, $_ unless $failed;

    print "    ".$name." (".$category."): Not available\n" if $failed;
}

print "\n";

for(keys %$categories)
{
    print "    $_\n";
    foreach my $value (@{%$categories->{$_}})
    {
        print "        ".%$modules->{$value}{name}.": Available (".%$modules->{$value}{values}.")\n";
    }
}
